package springboot.study.project.service.impl;

import org.apache.ibatis.session.RowBounds;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springboot.study.project.dto.StudentDto;
import springboot.study.project.dto.TempRecordDto;
import springboot.study.project.dto.VisitRecordDto;
import springboot.study.project.mapper.StudentMapper;
import springboot.study.project.mapper.TempRecordMapper;
import springboot.study.project.mapper.VisitRecordMapper;
import springboot.study.project.model.*;
import springboot.study.project.service.StudentService;
import springboot.study.project.service.TempService;
import springboot.study.project.util.common.StringUtil;
import springboot.study.project.util.common.Util;
import springboot.study.project.util.date.DateUtil;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName: TempServiceImpl
 * @Description: 体温接口实现类
 * @Author: liu-hao
 * @Date: 2021-03-28 16:49
 * @Version: 1.0
 **/
@Service
public class TempServiceImpl implements TempService {

    @Autowired
    TempRecordMapper tempRecordMapper;

    @Autowired
    VisitRecordMapper visitRecordMapper;

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    StudentService studentService;

    @Override
    public boolean addOrModifyTempRecord(TempRecord tempRecord) {
        String stuId = tempRecord.getStuId();
        Date recordDate = tempRecord.getRecordDate();
        String recordDateStr = DateUtil.getDateStr(DateUtil.PATTERN_NYR, tempRecord.getRecordDate());
        TempRecordExample example = new TempRecordExample();
        example.createCriteria().andStuIdEqualTo(stuId).andRecordDateEqualTo(recordDate);
        List<TempRecord> records = tempRecordMapper.selectByExample(example);
        boolean result = false;
        if (records == null || records.size() <= 0) {
            // 添加
            tempRecord.setGmtCreate(System.currentTimeMillis());
            tempRecord.setGmtModify(System.currentTimeMillis());
            int insertCount = tempRecordMapper.insert(tempRecord);
            result = insertCount > 0 ? true : false;
        } else {
            // 更新
            tempRecord.setGmtModify(System.currentTimeMillis());
            int updateCount = tempRecordMapper.updateByPrimaryKey(tempRecord);
            result = updateCount > 0 ? true : false;
        }
        return result;
    }

    @Override
    public boolean visitRegister(VisitRecord visitRecord) {
        if (visitRecord != null) {
            visitRecord.setRecordDate(new Date());
            int insertCount = visitRecordMapper.insert(visitRecord);
            return insertCount > 0 ? true : false;
        }
        return false;
    }

    @Override
    public Map<String, Object> findTempRecord(String stuName, String startTemp, String endTemp, int offset, int pageSize) {
        TempRecordExample example = new TempRecordExample();
        TempRecordExample.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotBlank(stuName)) {
            StudentExample studentExample = new StudentExample();
            studentExample.createCriteria().andStuNameLike("%" + stuName + "%");
            List<Student> students = studentMapper.selectByExample(studentExample);
            List<String> stuIds = students.stream().map(student -> student.getStuId()).collect(Collectors.toList());
            if (stuIds.size() <= 0)
                stuIds.add("!@#%^&*");
            criteria.andStuIdIn(stuIds);
        }
        if (StringUtil.isNotBlank(startTemp) && StringUtil.isNotBlank(endTemp)) {
            criteria.andStuTempBetween(startTemp, endTemp);
        } else if (StringUtil.isNotBlank(startTemp) && StringUtil.isBlank(endTemp)) {
            criteria.andStuTempBetween(startTemp, "999");
        } else if (StringUtil.isBlank(startTemp) && StringUtil.isNotBlank(endTemp)) {
            criteria.andStuTempBetween("0", endTemp);
        }
        // 查询符合条件体温计记录数量
        int totalCount = tempRecordMapper.countByExample(example);
        // 查询符合条件的体温计记录信息
        List<TempRecord> tempRecords = tempRecordMapper.selectByExampleWithRowbounds(example, new RowBounds(offset, pageSize));
        List<TempRecordDto> tempRecordDtos = new ArrayList<>();
        for (TempRecord tempRecord : tempRecords) {
            TempRecordDto tempRecordDto = new TempRecordDto();
            StudentDto studentDto = studentService.getStuInfoById(tempRecord.getStuId());
            if (studentDto == null)
                continue;
            BeanUtils.copyProperties(studentDto, tempRecordDto);
            BeanUtils.copyProperties(tempRecord, tempRecordDto);
            tempRecordDtos.add(tempRecordDto);
        }

        // 定义返回Map
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("msg", 0);
        resultMap.put("count", totalCount);
        resultMap.put("data", tempRecordDtos);
        return resultMap;
    }

    @Override
    public Map<String, Object> findVisitRecord(String stuName, String dormitoryId, int offset, int pageSize) {
        VisitRecordExample example = new VisitRecordExample();
        if (StringUtil.isNotBlank(stuName)) {
            StudentExample studentExample = new StudentExample();
            studentExample.createCriteria().andStuNameLike("%" + stuName + "%");
            List<Student> students = studentMapper.selectByExample(studentExample);
            List<String> stuIds = students.stream().map(student -> student.getStuId()).collect(Collectors.toList());
            if (stuIds.size() <= 0)
                stuIds.add("!@#%^&*");
            example.createCriteria().andStuIdIn(stuIds);
        }
        // 查询符合条件的访问登记记录数量
        int totalCount = visitRecordMapper.countByExample(example);
        // 查询符合条件的体温计记录信息
        List<VisitRecord> visitRecords = visitRecordMapper.selectByExampleWithRowbounds(example, new RowBounds(offset, pageSize));

        List<VisitRecordDto> visitRecordDtos = new ArrayList<>();
        for (VisitRecord visitRecord : visitRecords) {
            VisitRecordDto visitRecordDto = new VisitRecordDto();
            StudentDto studentDto = studentService.getStuInfoById(visitRecord.getStuId());
            if (studentDto == null)
                continue;
            BeanUtils.copyProperties(studentDto, visitRecordDto);
            BeanUtils.copyProperties(visitRecord, visitRecordDto);
            visitRecordDtos.add(visitRecordDto);
        }

        // 定义返回Map
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("msg", 0);
        resultMap.put("count", totalCount);
        resultMap.put("data", visitRecordDtos);
        return resultMap;
    }

}
